/*
 * Copyright 2009 BioTeam Inc
 * 
 * Licensed under License GNU LESSER GENERAL PUBLIC LICENSE version 3.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.gnu.org/copyleft/lesser.html
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package net.bioteam.appweb.submit;

import java.util.Calendar;
import java.util.Date;

import javax.persistence.EntityManager;
import javax.persistence.EntityTransaction;
import javax.persistence.Persistence;
import javax.persistence.Query;

import net.bioteam.appweb.Config;
import net.bioteam.appweb.TestConfig;
import net.bioteam.common.user.User;
import net.bioteam.common.user.UserInfo;
import net.bioteam.common.utils.MyEntityManager;
import junit.framework.TestCase;

public class SubmitPersistTester extends TestCase
{

	public SubmitPersistTester()
	{
		super();
	}

	public SubmitPersistTester(String name)
	{
		super(name);
	}

	protected void setUp() throws Exception
	{
		super.setUp();
		MyEntityManager.emf = Persistence.createEntityManagerFactory("appwebtest");
		Config.topdir = TestConfig.topdir;
	}

	protected void tearDown() throws Exception
	{
		super.tearDown();
		MyEntityManager.emf.close();
	}

	public static String userid = "xxx";

	public static void main(String args[])
	{
		SubmitPersistTester test = new SubmitPersistTester("testQuotaQuery");
		// ClusterUtilTester test = new ClusterUtilTester("testTemp");
		junit.textui.TestRunner.run(test);
	}
	public void testPersist()
	{
		EntityManager em = MyEntityManager.getEntityManager();
		EntityTransaction tx = em.getTransaction();

		UnfinishedJob job=new UnfinishedJob();
		job.setClusterid(-1);
		job.setCmdline("cmdline");
		job.setCode(RunStatusCode.PRESUBMIT);
		job.setEmail("user@exmaple.com");
		job.setExitValue(1);
		job.setFinishedTime(new Date());
		job.setJobid("53");
		job.setNote("test");
		job.setPreSubmitTime(new Date());
		job.setProgramName("test");
		job.setRemoteJobId("54");
		job.setTrials(1);
		job.setUsername(userid);
		job.setWorkdir("workdir");
		
		tx.begin();
		em.persist(job);
		tx.commit();
		
		RunStatus rs=em.find(RunStatus.class, job.getId());
		assertEquals(rs.getRemoteJobId(), job.getRemoteJobId());
		
		job.setCode(RunStatusCode.SUBMITTED);
		job.setSubmittedTime(new Date());
		tx.begin();
		em.flush();
		tx.commit();
		
		em.refresh(rs);
		assertEquals(rs.getCode(), job.getCode());
		assertEquals(rs.getSubmittedTime().getTime(), job.getSubmittedTime().getTime());
		
		tx.begin();
		em.remove(job);
		em.remove(rs);
		tx.commit();
		
		
		
	}
	
	public void testQuotaQuery() throws Exception
	{
		EntityManager em = MyEntityManager.getEntityManager();
		//admin user
		User user=em.find(User.class, 2L);
		if (user.getUserInfo().getDailyquote() != UserInfo.UNLIMITEDQUOTE)
		{
			
			Query totalQuery = em.createNamedQuery("jobfromtime");
			Calendar nowc = Calendar.getInstance();
			long start=nowc.getTimeInMillis();
			nowc.set(Calendar.SECOND, 0);
			nowc.set(Calendar.MINUTE, 0);
			nowc.set(Calendar.HOUR, 0);
			nowc.set(Calendar.MILLISECOND, 0);
			nowc.set(Calendar.AM_PM, Calendar.AM);
			long end=nowc.getTimeInMillis();
			
			System.err.println((start-end)/3600000);
			totalQuery.setParameter("presubmittime", nowc.getTime());
			
			totalQuery.setParameter("username", user.getIdAsString());
			try
			{
				Number result = (Number) totalQuery.getSingleResult();
				int count = result.intValue();
				System.err.println("count is "+count);
			} catch (javax.persistence.NoResultException e)
			{
				System.err.println("Exceprion: count is 0");
			}
		}
		em.close();
	}
	

	
}
